﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class en_products : System.Web.UI.Page
{
    class ProductTableRow
    {
        public int id = -1;
        public String Name = null;
        public String MajorType = null;
        public String MinorType = null;
        public String App = null;
        public String Benefits = null;
        public String SpecDoc = null;
        public String Image = null;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        String prodname = "";
        if (!IsPostBack)
        {
            prodname = this.Page.Request["Name"];
            String major = this.Page.Request["MajorType"];
            String minor = this.Page.Request["MinorType"];

            get_product_name();
            if (prodname != "" && prodname != null)
            {
                label_prodname.Text = prodname;
                get_product_major_type(prodname);
                label_minor_name.Text = prodname;
            }
            if (major != null && major != "" && minor != null && minor != "")
            {
                panel_minor_product.Visible = false;
                ProductTableRow row = read_product_detail(prodname, major, minor);
                if (row != null)
                {
                    fill_panel_minor_details(row);
                }
            }
            else
            {
                panel_minor_details.Visible = false;
            }
        }
    }

    private void fill_panel_minor_details(ProductTableRow row)
    {
        label_minor_name.Text = row.MinorType;
        if (label_minor_name.Text == "")
        {
            label_minor_name.Text = "No Such Type Exists";
        }
        hlink_prod_img.NavigateUrl = row.SpecDoc;
        hlink_prod_img.ImageUrl = row.Image;
        if (row.SpecDoc != null && row.SpecDoc != "")
        {
            //  hlink_prod_img.Text = "";
        }
        //bulletedlist_app
        if (row.App != null && row.App != "")
        {
            String[] lines = row.App.Replace("\r\n", "\n").Split('\n');

            bulletedlist_app.DataSource = lines;
            bulletedlist_app.DataBind();
        }
        //befefits
        if (row.Benefits != null && row.Benefits != "")
        {
            String[] lines = row.Benefits.Replace("\r\n", "\n").Split('\n');
            bulletedlist_befenits.DataSource = lines;
            bulletedlist_befenits.DataBind();
        }

        // spec doc
        hlink_prod_doc.NavigateUrl = row.SpecDoc;
    }

    private ProductTableRow read_product_detail(String prodname, String major, String minor)
    {
        String select = "";
        try
        {
            select = "SELECT distinct MinorType, App, Benefits, SpecDoc, Image FROM en_ProductTable ";
            select += " WHERE Name = '{0}' and MajorType='{1}' and MinorType='{2}' ";
            select = String.Format(select, prodname, major, minor);

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString);
            SqlCommand cmd = new SqlCommand(select, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            List<String> details = new List<String>();
            ProductTableRow row = null;
            if (reader.Read())
            {
                row = new ProductTableRow();
                row.MinorType = reader.GetValue(0) as String;
                row.App = reader.GetValue(1) as String;
                row.Benefits = reader.GetValue(2) as String;
                row.SpecDoc = reader.GetValue(3) as String;
                row.Image = reader.GetValue(4) as String;
            }
            reader.Close();
            conn.Close();
            return row;
        }
        catch (Exception)
        {
            ShowAlertMessage(
                String.Format("Failed to Connect Database, Connect String={0}, SQL Query={1}!", ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString, select)
                );
        }
        return null;
    }

    private void get_product_name()
    {
        String select = "SELECT distinct Name FROM en_ProductTable";
        try
        {
            SqlDataSource sqlds = new SqlDataSource(ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString, select);
            gv_prodmenu.DataSource = sqlds;
            gv_prodmenu.DataBind();
        }
        catch (Exception)
        {
            ShowAlertMessage(
                String.Format("Failed to Connect Database, Connect String={0}, SQL Query={1}!", ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString, select)
                );
        }
        return;
    }

    private void get_product_major_type(String prodname)
    {
        String select = "";
        try
        {
            select = String.Format("SELECT distinct Name, MajorType FROM en_ProductTable WHERE Name = '{0}'", prodname);
            SqlDataSource sqlds = new SqlDataSource(ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString, select);
            gv_product_major_type.DataSource = sqlds;
            gv_product_major_type.DataBind();
        }
        catch (Exception err)
        {
            ShowAlertMessage(String.Format("Failed to Connect Database, Connect String={0}, SQL Query={1}!, Error:" + err.ToString(),
                                ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString,
                                select)
                );
        }
        return;
    }

    protected void OnRowDataBound_gv_product_major_type(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            String major = DataBinder.Eval(e.Row.DataItem, "MajorType") as String;
            GridView gridChild = (GridView)e.Row.Cells[0].Controls[1]; // gv_product_minor_type
            gridChild.DataSource = make_minor_ds(label_prodname.Text, major);
            gridChild.DataBind();
        }
    }

    protected SqlDataSource make_minor_ds(String prodname, String major)
    {
        //SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString);
        String select = String.Format("SELECT distinct Name, MajorType, MinorType FROM en_ProductTable WHERE Name = '{0}' and MajorType='{1}'", prodname, major);
        SqlDataSource sqlds = new SqlDataSource(ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString, select);
        return sqlds;


    }



    private void ShowAlertMessage(string message)
    {
        Page page = HttpContext.Current.Handler as Page;

        if (page != null)
        {
            message = message.Replace("'", "\'");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + message + "');", true);
        }
    }

}